package day13;

import java.io.*;

/*本类用于测试字符输出流*/
public class TestWriter {
    public static void main(String[] args) {
        method();//用于测试普通字符输出流
        method2();//用于测试高效字符输出流
    }
    private static void method() {
        Writer out = null;
        try{//放着的是可能会出现异常的代码
            //1.创建流对象
            //Writer out2 = new FileWriter(new File("D:\\ready\\2.txt"));
            out = new FileWriter("D:\\ready\\2.txt");
            //2.使用流对象写出数据
            out.write(100);
            out.write(101);
            out.write(102);
            //2.2刷一下数据
            out.flush();

        }catch (Exception e){//用来匹配并捕获异常
            e.printStackTrace();
        }finally {//本结构中一定会执行到的代码块,必要操作比如关流就可以放到这里
            //3.释放资源
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static void method2() {
        Writer out = null;
        try{
            //1.创建流
            //out = new BufferedWriter(new FileWriter("D:\\ready\\2.txt"));
            out = new BufferedWriter(new FileWriter("D:\\ready\\2.txt",true));
            //2.使用字符输出流输出数据
            out.write(97);
            out.write(98);
            out.write(99);
            //3.刷一下数据,防止遗漏
            out.flush();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
